<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/9
 * Time: 12:38
 */

namespace App;


use Illuminate\Database\Eloquent\Model;

class VipCardRuleValue extends Model
{
    //指定表名
    protected $table='vip_card_rule_value';
    // 开启自动时间戳
    public $timestamps = true;
    // 日期都存储为时间戳
    protected $dateFormat = 'U';

    protected $appends = ['discount_integer', 'discount_decimal'];

    // 拼接结束日期时间字段
    public function getDiscountAttribute()
    {
        return ($this->attributes['discount']/10);
    }

    // 将折扣从小数转换为整数
    public function setDiscountAttribute($value)
    {
        $this->attributes['discount'] =  $value*10;
    }

    // 获取折扣的整数部分
    public function getDiscountIntegerAttribute()
    {
        return substr( $this->attributes['discount'], 0, 1 );
    }

    // 获取折扣的小数部分
    public function getDiscountDecimalAttribute()
    {
        return substr( $this->attributes['discount'], -1);
    }

    public function vipCard()
    {
        return $this->hasOne('App\VipCard', 'id', 'vip_card_id');
    }
}